<?php

namespace app\common\model;

use app\common\exception\FailedException;
use think\Model;

class BaseModel extends Model
{
    protected $connection = 'gii';
    protected $autoWriteTimestamp = 'Y-m-d H:i:s';

    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    public function getStatusTextAttr($value, $data)
    {
        $list = $this->getStatusTextList();
        return $list[$data['status']] ?? "未知";
    }

    /**
     * Notes:检查数据是否存在
     * @param $condition //数组或者直接传id
     * @param $field //查询字段
     * @param $append //闭包
     * @param $func //闭包
     * @return array|object
     */
    public static function checkBaseInfo($condition, $field = ['*'], $append=[], $func = [], $with=[], $msg=""):object|array
    {
        $where = is_array($condition) ? $condition : [['id', '=', $condition]];
        $info = self::where($where)->where($func)->field($field)->with($with)->append($append)->find();
        if(!$info){
            throw new FailedException($msg??"数据不存在");
        }
        return $info;
    }


}